//全局变量
var c=null;
var ctx=null;
var QM=20;
var MaxScore=null; //最大值
var PlayTimes=null; //次数


//页面加载
window.onload=function(){
	c=document.getElementById("canvas");
	ctx=c.getContext("2d");
	ctx.save();
	
	//调整画布
	c.width=window.innerWidth;
	c.height=window.innerHeight;
	window.onresize=function(){
		c.width=window.innerWidth;
		c.height=window.innerHeight;
	}
	
	if(!sessionStorage.PlayTimes){
		sessionStorage.setItem("PlayTimes",0);
	}if(!localStorage.MaxScore){
		localStorage.setItem("MaxScore",0);
	}
	
	//加载游戏
	new Game().gameLoad();
}

//键盘对象
var keys={
	front:0,//队头指针
	rear:0,//队尾指针
	ks:null,//队列数组
	status:false,//键盘侦听不启用
	set:function(){//初始化队列
		keys.ks=new Array(QM);
		keys.front=0; //队头设置为空
		keys.rear=0; //队尾设置为空
		keys.status=true;
	},
	enQ:function(key){//入队操作
		if((keys.rear+1)%QM!=keys.rear){//队列未满
			keys.ks[keys.rear]=key;
			keys.rear=(keys.rear+1)%QM;
			return true;
		}
		return false;
	},
	delQ:function(){//出队操作
		if(keys.front!=keys.rear){//队列不为空
			var k=keys.ks[keys.front];
			keys.front=(keys.front+1)%QM;
			return k;
		}
		return null;
	}
}

//游戏
function Game(){
	this.gameStatus;
	this.scene;
	this.maxStars;
	this.score;
	this.ship;
	this.bulletArr;
	this.starArr;
	this.emitters;
	this.timeHandle;
	this.timeControl;
	this.timeLimit;
	this.music1;
	this.music2;
	this.booMusic1;
	this.booMusic2;
	this.launchMusic;
	this.winMusic;
	this.loseMusic;
	this.muted=false;
};
Game.prototype.gameLoad=function(){
	this.gameStatus="begin";
	var self=this;
	this.starArr=new Array(); //创建陨石数组
	for(var i=1;i<Math.random()*10+10;i++){
		var star=new Star(Math.random()*c.width,Math.random()*c.height,Math.random()*50+50);
		star.set();
		this.starArr.push(star);
	}
	this.scene=new Scene();
	window.onkeydown=function(e){ //键控
		var k=e.which;
		console.log(k);
		if(k==32&&keys.status==false){ //游戏未开始
			self.gameSet();
		}
		else if(keys&&keys.status){ //游戏开始
			if(k){
				keys.enQ(k);
			}
		}
		if(k==107){ //Num+键：静音
			self.muted=!self.muted;
			self.music1.setMuted(self.muted);
			self.music2.setMuted(self.muted);
			self.booMusic1.setMuted(self.muted);
			self.booMusic2.setMuted(self.muted);
			self.winMusic.setMuted(self.muted);
			self.loseMusic.setMuted(self.muted);
		}
	};

	if(this.music1==null){ //初始音乐
		this.music1=new GameAudio("bg1","audio/wangling.mp3",true);
	}
	if(this.music2==null){ //游戏进行音乐
		this.music2=new GameAudio("bg2","audio/siji.mp3",true);
	}
	if(this.booMusic1==null){ //飞船爆炸音效
		this.booMusic1=new GameAudio("boo1","audio/booship.mp3",false);
	}
	if(this.booMusic2==null){ //陨石爆炸音效
		this.booMusic2=new GameAudio("boo2","audio/boobullet.mp3",false);
	}
	if(this.winMusic==null){ //获胜音效
		this.winMusic=new GameAudio("win","audio/win.mp3");
	}
	if(this.loseMusic==null){ //失败音效
		this.loseMusic=new GameAudio("lost","audio/lose2.mp3");
	}
	
	if(this.timeHandle){ //调用时间函数
		cancelAnimationFrame(this.timeHandle);
	}

	this.gameRender();//渲染游戏画面
};

Game.prototype.gameRender=function(){
	ctx.restore(); //还原画布
	ctx.clearRect(0,0,c.width,c.height); //清除画布
	ctx.save(); //保存画布
	
	switch(this.gameStatus){
		case "begin": //初始界面
			this.music1.play(); //初始界面背景音乐
			this.loseMusic.pause(); //初始界面其他音乐
			this.winMusic.pause();
			this.scene.beginScene(); //初始界面场景
			break;
		
		case "play": //游戏界面
			this.music1.pause(); //游戏界面其他音乐
			this.music2.play(); //游戏界面背景音乐
			this.timeControl.draw();
			if(this.ship.life){ //飞船更新及绘制
				this.ship.update(this.bulletArr);
				this.ship.draw();
				this.timeControl.update();
    		}
			if(this.bulletArr){ //子弹更新及绘制
				for(var i=0;i<this.bulletArr.length;i++){
					this.bulletArr[i].draw();
					if(this.bulletArr[i].maxs<0){ //子弹超出射程 清除
						this.bulletArr[i]=null;
						this.bulletArr.splice(i,1);
					}else{
						this.bulletArr[i].update(); //否 则继续更新
					}
				}
			}
			//陨石更新及绘制
			if(this.starArr){
				for(var i=0;i<this.starArr.length;i++){
					if(this.starArr[i].life){
						this.starArr[i].update();
						this.starArr[i].draw();
						if(this.ship.life&&pengzhuang1(this.starArr[i],this.ship,this.emitters)){ //飞船与陨石碰撞
							this.ship.life=false;
							this.starArr[i].life=false;
							this.booMusic1.play(); //播放音效
						}
						for(var j=0;j<this.bulletArr.length;j++){
							if(this.starArr[i]&&this.bulletArr.length&&pengzhuang2(this.starArr[i],this.bulletArr[j],this.emitters)){ //子弹与陨石碰撞
								this.starArr[i].life=false;
								// this.bulletArr[i]=null;
								// this.bulletArr.splice(i,1);
								// this.bulletArr[i].life=false;
								this.starArr.splice(i,1);
								this.score++; //
								this.booMusic2.play(); //播放音效
							}
						}
					}
				}
			}
			//发射器更新
			for(var i=0;i<this.emitters.length;i++){
				this.emitters[i].update();
				if(this.emitters[i].life==false){
					this.emitters.splice(i,1);
				}
			}
			if(this.emitters.length==0){
				if(this.ship.life==false){
					this.gameStop("lost");
				}else if(this.starArr.length==0){
					this.gameStop("win");
				}
			}
			if(this.timeLimit<=this.timeControl.currTime){
				if(this.starArr.length==0){
					this.gameStop("win");
				}else{
					this.gameStop("lost");
				}					
			}
			this.scene.playScene(this.maxStars,this.score,this.timeControl.fps,this.timeControl.currTime);
			break;
		
		case "win": //获胜场景
			this.scene.winScene(this.score,this.timeControl.currTime);
			this.music2.pause();
			this.winMusic.play();
			break;
		
		case "lost": //失败场景
			this.scene.lostScene(this.score,this.maxStars-this.score,this.timeControl.currTime);
			this.loseMusic.play();
			this.music2.pause();
			this.booMusic1.pause();
			break;
	}
	this.timeHandle=requestAnimationFrame(Game.prototype.gameRender.bind(this));
};

Game.prototype.gameSet=function(){ //游戏初始化
	this.gameStatus="play";
	keys.set(); //启用键盘侦听
	this.ship=new Ship(c.width*0.5,c.height*0.5,18,45);
	this.bulletArr=new Array(); //定义子弹数组
	this.starArr=new Array(); //将流星定义在一个数组中
	for(var i=0;i<Math.random()*10+5;i++){ //陨石数量在5-15之间
		var star=new Star(Math.random()*c.width,Math.random()*c.height,Math.random()*20+20);
		star.set();
    	this.starArr.push(star);//向陨石数组末尾添加新元素，并返回新长度
    }
    this.emitters=new Array(); //发射器数组
    this.timeControl=new FrameSet();
	//LimitRect(20,105,150,50,timeLimit);
    this.maxStars=this.starArr.length;
    this.score=0;
    this.timeLimit=60;
};

Game.prototype.gameStop=function(status){ //游戏结束
	this.gameStatus=status;	
	var self=this;
	keys.status=false; //关闭键盘侦听
	if(this.score>localStorage.MaxScore){ //存储最高分
		localStorage.MaxScore=this.score;
	}
	sessionStorage.PlayTimes=Number(sessionStorage.PlayTimes)+1; //更新游戏运行次数
	window.onkeydown=function(e){
		if(e.which){
			self.gameLoad();
		}
	};
	this.timeHandle=cancelAnimationFrame(self.timeHandle);
};

//两点之间的距离
function disc(x1,y1,x2,y2){
	return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

//碰撞检测
function pengzhuang1(star,ship,emitters){ //飞船与陨石碰撞
	var x1=ship.x+Math.sin(ship.angle)*ship.sh;
	var y1=ship.y-Math.cos(ship.angle)*ship.sh;
	var x2=star.x;
	var y2=star.y;
	var r=star.r;
	if(disc(x1,y1,x2,y2)<r){ //A点发生碰撞
		var emitter=new Emitter(200,1,x1,y1,"circle","#FFF",8);
		emitters.push(emitter);
		return true;
	}
	x1=ship.x+Math.cos(ship.angle)*ship.sw;
	y1=ship.y+Math.sin(ship.angle)*ship.sw;

	if(disc(x1,y1,x2,y2)<r){ //B点发生碰撞
		var emitter=new Emitter(200,1,x1,y1,"circle","#FFF",8);
		emitters.push(emitter);
		return true;
	}
	x1=ship.x-Math.cos(ship.angle)*ship.sw;
	y1=ship.y-Math.sin(ship.angle)*ship.sw;

	if(disc(x1,y1,x2,y2)<r){ //D点发生碰撞
		var emitter=new Emitter(200,1,x1,y1,"circle","#FFF",8);
		emitters.push(emitter);
		return true;
	}
}

function pengzhuang2(star,zidan,emitters){ //子弹与流星碰撞
	var x1=zidan.x;
	var y1=zidan.y;
	var x2=star.x;
	var y2=star.y;
	var r=star.r;
	if(disc(x1,y1,x2,y2)<r){ //在子弹起点碰撞
		var emitter=new Emitter(80,1,x2,y2,"rect","rgb(255,0,0)",15);
		emitters.push(emitter);
		return true;
	}
	x1=zidan.x+Math.sin(zidan.angle)*zidan.h;
    y1=zidan.y-Math.cos(zidan.angle)*zidan.h;

    if(disc(x1,y1,x2,y2)<r){ //在子弹终点碰撞
    	var emitter=new Emitter(80,1,x2,y2,"rect","rgb(255,0,0)",15);
		emitters.push(emitter);
		return true;
	}
}

//飞船类
var Ship=function(x,y,sw,sh){
	this.x=x;
	this.y=y;
	this.sw=sw;
	this.sh=sh;
	this.speed=0;//速度
	this.angle=0;//角度
	this.life=true;
};

Ship.prototype.draw=function(){ //飞船绘制
	ctx.fillStyle="rgb(0,200,0)";
	ctx.beginPath();
	ctx.moveTo(
		this.x+Math.sin(this.angle)*this.sh,
		this.y-Math.cos(this.angle)*this.sh
	);
	ctx.lineTo(
		this.x+Math.cos(this.angle)*this.sw,
		this.y+Math.sin(this.angle)*this.sw
	);
	ctx.lineTo(
		this.x+Math.sin(this.angle)*this.sh/3,
		this.y-Math.cos(this.angle)*this.sh/3
	);
	ctx.lineTo(
		this.x-Math.cos(this.angle)*this.sw,
		this.y-Math.sin(this.angle)*this.sw
	);
	ctx.closePath();
	ctx.fill();
};

Ship.prototype.update=function(bulletArr){ //飞船更新
	var k=null;
	this.x+=Math.sin(this.angle)*this.speed; //x向移动距离
	this.y-=Math.cos(this.angle)*this.speed; //y向移动距离
	
	//边界检测
	if(this.x<this.sh/2||this.x>c.width-this.sh/2){
		this.angle=-this.angle;
	}else if(this.y<this.sh/2||this.y>c.height-this.sh/2){
		this.angle=Math.PI-this.angle;
	}
	
	if(keys&&keys.status){ //键盘侦听启用 则接收控制
		var k=keys.delQ();
	}
	switch(k){
		case 37://左键：逆转
			this.angle-=Math.PI/20;
			break;
		case 38://上键：加速
			if(this.speed<8){
				this.speed+=1;
			}
			break;
		case 39://右键：顺转
			this.angle+=Math.PI/20;
			break;
		case 40://下键：反向
			this.angle+=Math.PI;
			break;
		case 96://0键：暂停
			this.speed=0;
			break;
		case 17://ctrl键：减速
			if(this.speed>0)
				this.speed-=1;
			break;
		case 13://回车键：瞬移
			this.x=Math.random()*c.width;
			this.y=Math.random()*c.height;

			for(var i=0;i<10;i++){
				zidan=new Zidan(
					this.x+Math.sin(this.angle)*this.sh,
					this.y-Math.cos(this.angle)*this.sh,
					this.angle+i*36
				);
				//向子弹数组末尾添加新的子弹元素，并返回新的长度
				bulletArr.push(zidan);
			}
			break;
		case 32://空格键：发射子弹
			zidan=new Zidan(
				this.x+Math.sin(this.angle)*this.sh,
				this.y-Math.cos(this.angle)*this.sh,
				this.angle
			);
			//向子弹数组末尾添加新的子弹元素，并返回新的长度
			bulletArr.push(zidan);
			
			if(this.launchMusic==null){ //子弹发射音效
				this.launchMusic=new GameAudio("launch","audio/zidan.mp3",false);
			}
			this.launchMusic.play();
			break;
	}
};

//子弹类
var Zidan=function(x,y,angle){
	this.x=x;
	this.y=y;
	this.angle=angle;
	this.h=20; //子弹长度
	this.speed=10; //子弹速度
	this.maxs=500;//子弹最大射程
};

Zidan.prototype.draw=function(){ //子弹绘制
	ctx.strokeStyle="yellow";
	ctx.lineWidth=4;
	ctx.beginPath();
	ctx.moveTo(this.x,this.y);
	ctx.lineTo(
		this.x+Math.sin(this.angle)*this.h,
		this.y-Math.cos(this.angle)*this.h
	);
	ctx.closePath();
	ctx.stroke();
};

Zidan.prototype.update=function(){ //子弹更新
	this.x+=Math.sin(this.angle)*this.speed; //x向移动距离
	this.y-=Math.cos(this.angle)*this.speed; //y向移动距离
	this.maxs-=this.speed;
};

//陨石类
var Star=function(x,y,r){
	this.x=x;
	this.y=y;
	this.r=r;
	this.vs=null; //vs数组代表多边形n个顶点
	this.speedX=0;
	this.speedY=0;
	this.life=true;
};

Star.prototype.set=function(){ //陨石外观
	var n=Math.round(Math.random()*15)+6;//生成一个随机数，代表顶点个数n,必须大于3
	this.vs=new Array();//创建数组
	for(var i=0;i<n;i++){//生成n个随机角度（0~2*PI）
		var a=Math.random()*Math.PI*2;
		this.vs.push(a);
	}
	this.vs.sort(); //角度排序
	this.speedX=Math.random()*6-2; //生成随机速度
	this.speedY=Math.random()*6-2;
};

Star.prototype.draw=function(){ //陨石绘制
	ctx.strokeStyle="red";
	ctx.lineWidth=3;
	ctx.beginPath();
	ctx.moveTo(
		this.x+this.r*Math.sin(this.vs[0]),
		this.y+this.r*Math.cos(this.vs[0])
		);
	for(var i=1;i<this.vs.length;i++){
		ctx.lineTo(
			this.x+this.r*Math.sin(this.vs[i]),
			this.y+this.r*Math.cos(this.vs[i])
		);
	}
	ctx.closePath();
	ctx.stroke();
};

Star.prototype.update=function(){ //陨石更新
	this.x+=this.speedX;//x方向移动距离
	this.y+=this.speedY;//y方向移动距离
	//边界检测
	if(this.x>c.width||this.x<0){
		this.speedX=-this.speedX;
		this.x+=this.speedX;
	}else if(this.y>c.height||this.y<0){
		this.speedY=-this.speedY;
		this.y+=this.speedY;
	}
};

//粒子类
var Paticle=function(x0,y0/*位置*/,shape,color,size/*大小*/,lstatus,vstatus){
	this.x=x0; 
	this.y=y0;
	this.vx=Math.random()*10-2; /*速度*/
	this.vy=Math.random()*10-2;
	this.size=size; 
	this.shape=shape; //形状
	this.gravity=0; //重力
	this.color=color;
	this.life=Math.random()*50+10;
	this.lstatus=lstatus;
	this.vstatus=vstatus;
};

Paticle.prototype.draw=function(){ //粒子绘制
	ctx.fillStyle=this.color;
	ctx.beginPath();
	if(this.shape=="circle"){
 		ctx.arc(this.x,this.y,this.size,0,Math.PI*2);
	}
	else if(this.shape=="rect"){
		ctx.rect(this.x,this.y,this.size,this.size);
	}
	ctx.closePath();
	ctx.fill();
	this.update();
};
Paticle.prototype.update=function(){
	if(this.vstatus){
		this.x+=this.vx;
		this.y+=this.vy;
		this.vy+=this.gravity;
		this.size=Math.abs(this.size-0.3);
	}
	if(this.lstatus){
		this.life=this.life-1.5;
	}
};

//发射器
var Emitter=function(len,type,x0,y0,shape,color,size){
	this.len=len;//粒子数量
	this.paticles=new Array();
	this.type=type;  //发射类型 爆发式等等
	this.life=true;
	this.shape=shape;
	this.size=size;
	this.init(x0,y0,shape,color,size);
};
Emitter.prototype.init=function(x0,y0,shape,color,size){
	if(this.type==0){
		for(var i=0;i<this.len;i++){
			var p=new Paticle(x0,y0,shape,color,20,true,true);
			this.paticles.push(p);
		}
	}else if(this.type==1){
		for(var i=0;i<this.len;i++){
			var color="rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
			var p=new Paticle(x0,y0,shape,color,size*Math.random(),true,true);
			this.paticles.push(p);
		}
	}
	else if(this.type==2){
		for(var i=0;i<this.len;i++){
			var color="rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
			var p=new Paticle(Math.random()*c.width,Math.random()*c.height,this.shape,color,Math.random()*this.size,true,false);
			this.paticles.push(p);	
		}
	}
};
Emitter.prototype.update=function(){
	for(var i=0;i<this.paticles.length;i++){
		this.paticles[i].draw();
		if(this.paticles[i].life<=0){
			if(this.type==2){
				if(this.paticles[i].lstatus){
					var color="rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")";
					var p=new Paticle(Math.random()*c.width,Math.random()*c.height,this.shape,color,Math.random()*this.size,true,false);
					this.paticles[i]=p;
				}	
			}else{
				this.paticles.splice(i,1);
			}	
		}
	}
	this.len=this.paticles.length;
	if(this.len<=0){
		this.life=false;
		console.log(this.life);
	}
};

//场景类
function Scene(){
	this.nameText=new Array("太","空","战","舰");
	this.controlText1="“Space” 开始游戏";
	this.controlText2="“Num +” 关闭或开启背景音乐";
	this.helpText="方向键控制  空格发射子弹  回车释放技能  详见右下角";
	this.extraText="Copyright © 2013-2017 李明红 090414102 All Rights Reserved. 备案号：闽ICP备15012807号-1";
	this.controlText=["Enter 瞬移+技能","Num0 定格","Ctrl 减速","Space 射击","→ 顺时针转","← 逆时针转","↓ 掉头","↑ 加速","Num+ 音乐"];
	this.winText="恭喜您  挑战成功";
	this.loseText="很遗憾  挑战失败";
	this.continueText="按任意键继续游戏";
	this.bg=new Emitter(1500,2,0,0,"rect","#FFF",3);
	this.colors=["rgba(0,200,100,1)","rgba(0,200,100,0.66)","rgba(0,200,100,0.33)"];
	this.fps=0;            
	this.colorindex=0;

};
//游戏开始
Scene.prototype.beginScene=function(){
	this.bg.update();
	this.fps++;
	if(this.fps>=30){
		this.colorindex=(this.colorindex+1)%this.nameText.length;//依次取
		this.fps=0;
	}
	ctx.restore();
	ctx.save();
	ctx.textAlign="center";
	ctx.font="120px fantasy";
	ctx.lineWidth=2;
	for(var i=0;i<this.nameText.length;i++){
		if(this.colorindex==i){
			ctx.strokeStyle=this.colors[0];
		}else{
			ctx.strokeStyle=this.colors[1];
		}
		//ctx.strokeStyle=this.colors[(i+this.colorindex)%this.colors.length];
		ctx.strokeText(this.nameText[i],c.width*0.5-300+i*200,c.height*0.45);
	}

	ctx.textAlign="center";
	ctx.font="lighter 20px cursive";
	ctx.fillStyle="rgba(0,200,200,1)";
	ctx.fillText(this.controlText1,c.width*0.5,c.height*0.58);
	ctx.fillText(this.controlText2,c.width*0.5,c.height*0.65);
	ctx.fillText(this.helpText,c.width*0.5,c.height*0.72);
	ctx.font="lighter 10px cursive";
	ctx.fillStyle="rgba(200,200,0,1)";
	ctx.fillText(this.extraText,c.width*0.5,c.height-20);
};
//游戏过程
Scene.prototype.playScene=function(starArr,shotted,fps,times){
	this.bg.update();
	ctx.restore();
	ctx.save();

	ctx.textAlign="left";
	ctx.font="15px cursive";
	// ctx.fillStyle="#FFB90F";
	ctx.fillStyle="yellow";
	for(var i=1;i<this.controlText.length;i++){
		ctx.fillText(this.controlText[i],c.width-100,c.height-20-i*30);
	}
	ctx.font="20px cursive bold";
	ctx.fillStyle="green";
	ctx.fillText(this.controlText[0],c.width-150,c.height-15);

	ctx.font="15px cursive";
	ctx.fillStyle="yellow";
	ctx.fillText("Stars: "+starArr+", Shotted: "+shotted,30,30);
	ctx.fillText("Fps: "+fps+", Times: "+times+"s",30,60);

	ctx.font="15px cursive";
	ctx.fillStyle="yellow";
	ctx.fillText("MaxScore: "+localStorage.MaxScore,c.width-130,30);
	ctx.fillText("PlayTimes: "+sessionStorage.PlayTimes,c.width-130,60);
	
	//LimitRect(20,105,150,50,timeLimit);
};
//游戏结束-胜利
Scene.prototype.winScene=function(shotted,times){
	this.bg.update();
	ctx.restore();
	ctx.save();
	ctx.beginPath();
	ctx.fillStyle="rgba(255,255,255,0.15)";
	ctx.rect(c.width*0.3,c.height*0.3,c.width*0.4,c.height*0.4);
	ctx.fill();
	ctx.textAlign="center";

	ctx.font="50px cursive";
	ctx.fillStyle="rgba(0,200,200,1)";
	ctx.fillText(this.winText,c.width/2,c.height/2-50);

	ctx.font="25px cursive";
	ctx.fillStyle="rgba(200,200,200,1)";
	ctx.fillText("击中目标: "+shotted+"   耗时: "+times+"s",c.width/2,c.height/2+30);

	ctx.font="18px cursive";
	ctx.fillStyle="rgba(200,200,200,1)";
	ctx.fillText(this.continueText,c.width/2,c.height/2+100);

};
//游戏结束-失败
Scene.prototype.lostScene=function(shotted,starArr,times){
	this.bg.update();
	ctx.restore();
	ctx.save();
	ctx.beginPath();
	ctx.fillStyle="rgba(255,255,255,0.15)";
	ctx.textAlign="center";
	ctx.rect(c.width*0.3,c.height*0.3,c.width*0.4,c.height*0.4);
	ctx.fill();
	
	ctx.font="50px cursive";
	ctx.fillStyle="rgba(0,200,200,1)";
	ctx.fillText(this.loseText,c.width/2,c.height/2-50);

	ctx.font="25px cursive";
	ctx.fillStyle="rgba(200,200,200,1)";
	ctx.fillText("击中目标: "+shotted+"   剩余目标: "+starArr+"   耗时: "+times+"s",c.width/2,c.height/2+30);

	ctx.font="18px cursive";
	ctx.fillStyle="rgba(200,200,200,1)";
	ctx.fillText(this.continueText,c.width/2,c.height/2+100);
};
//计时类
function FrameSet(){
	 this.fps;
	 this.currTime; //当前时间
	 this.startTime; //开始时间
	 this.finishTime; //结束时间
	 this.limitTime; //时间限制
	 this.init();
};
FrameSet.prototype.init=function(){
	this.fps=0;
	this.currTime=0;
	this.startTime=new Date();
	this.limitTime=60;
};
FrameSet.prototype.update=function(){
	this.fps++;
	this.finishTime=new Date();
	if(this.finishTime-this.startTime>1000){
		this.currTime++;
		this.startTime=this.finishTime;
		this.limitTime--;
	}
};

FrameSet.prototype.draw=function(){
	ctx.clearRect(10,55,250,20);
	ctx.lineWidth=3;

	//电池
	ctx.fillStyle="rgba(0,200,0,1)";
	ctx.beginPath();
    ctx.moveTo(30,100);
    ctx.lineTo(180,100);
    ctx.lineTo(180,130);
    ctx.lineTo(30,130);
	ctx.closePath();
    ctx.fill();
	
	ctx.fillStyle="#000";
	ctx.beginPath();
	ctx.rect(30+150-this.currTime/2*5,100,150,30);
	ctx.closePath();
	ctx.fill();
	
	ctx.strokeStyle="rgba(200,200,0,1)"
	ctx.beginPath();
    ctx.moveTo(30,100);
    ctx.lineTo(180,100);
    ctx.lineTo(180,130);
    ctx.lineTo(30,130);
	ctx.closePath();
    ctx.stroke();
	ctx.fillStyle="rgba(200,200,0,1)";
	ctx.beginPath();
	ctx.rect(180,110,10,10);
	ctx.closePath();
	ctx.fill();

	//倒计时字
	ctx.fillStyle="rgba(200,200,0,1)";
	ctx.font="15px cursive";
	ctx.fillText("剩余时间：",30,88);
	ctx.fillText(60-this.currTime+"s",100,88);
};

//倒计时画图
function LimitRect(x,y,wd,wh,limitTime){
	ctx.strokeStyle="#FFF";
	ctx.lineWidth=2;
	ctx.strokeRect(x,y,wd,wh);
	ctx.fillStyle="#FFF";
	//ctx.fillRect(x+wd,y+1/4*wh,1/6*wh,1/2*wh);
	for(var i=0;i<limitTime;i++){
		ctx.fillStyle="rgba("+255*(30-i)/30+","+255*i/30+",0,1)";
		ctx.fillRect(x+4,y+4,(wd-8)*i/30,wh-8);
	}
}

//声音控制类
function GameAudio(id,file,loop){
	this.player;
	var e=document.getElementById(id);
	if(e){
		document.body.removeChild(e);
	}
	if(typeof(file)!=undefined){
		this.player=document.createElement("audio");
		this.player.id=id;
		if(loop){
			this.player.setAttribute("loop","loop");
		}
		document.body.appendChild(this.player);
		var e=document.createElement("source");
		e.src=file;
		e.type="audio/mpeg";
		this.player.appendChild(e);
	}
};
GameAudio.prototype.play=function(){
	this.player.play();
};
GameAudio.prototype.pause=function(){
	this.player.pause();
};
GameAudio.prototype.stop=function(){
	this.player.stop();
};
GameAudio.prototype.setMuted=function(muted){
	this.player.muted=muted;
};
